package com.upc.feedish.ui.notifications;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.upc.feedish.R;
import com.upc.feedish.ShoppingCartBean;
import com.upc.feedish.shopp.ShoppingCartActivity;

import java.util.ArrayList;
import java.util.List;
//import com.upc.feedish.databinding.FragmentNotificationsBinding;

public class NotificationsFragment extends Fragment {
    private View root;
    private ListView listView;
    private Button tv;
    public static ShoppingCartBean shoppingCartBean = new ShoppingCartBean();
    public static List<ShoppingCartBean> shoppingCartBeanList = new ArrayList<>();
    //    private ShoppingCartAdapter shoppingCartAdapter = new ShoppingCartAdapter();
//    shoppingCartAdapter.setShoppingCartBeanList(shoppingCartBeanList);
    //private Button shop_btn;
    private static String[] titles = {"金丝鱼", "安康鱼", "金鱼", "多宝鱼", "金龙鱼", "白雕", "罗汉鱼"};
    private static String[] prices = {"1800", "10", "300", "350", "10", "280", "30"};
    private static int[] icons = {R.drawable.jinsi, R.drawable.ankang, R.drawable.jinyu,
            R.drawable.duobao, R.drawable.jinlong, R.drawable.baidiao, R.drawable.luohan};

    //    private NotificationsViewModel notificationsViewModel;
//    private FragmentNotificationsBinding binding;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
//        notificationsViewModel =
//                new ViewModelProvider(this).get(NotificationsViewModel.class);
//
//        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
//        View root = binding.getRoot();
        if(root==null){
            root=inflater.inflate(R.layout.fragment_notifications,container,false);
        }
        tv = root.findViewById(R.id.cart);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ShoppingCartActivity.class);
                startActivity(intent);
            }
        });
        //初始化ListView控件
        listView = root.findViewById(R.id.lv);
        //创建一个Adapter的实例
        NotificationsFragment.MyBaseAdapter mAdapter = new NotificationsFragment.MyBaseAdapter();
        //设置Adapter
        listView.setAdapter(mAdapter);
        return root;
    }
    class ViewHolder {
        TextView title;
        TextView price;
        ImageView iv;
        Button shop_btn;
    }

    public static class MyOnClickListener implements View.OnClickListener {
        private static NotificationsFragment.MyOnClickListener instance = null;
        public static int position;
        //    System.out.println(position);
        private MyOnClickListener() {
        }
        public static NotificationsFragment.MyOnClickListener getInstance() {
            if (instance == null)
                instance = new NotificationsFragment.MyOnClickListener();
            return instance;
        }
        @Override
        public void onClick(View view) {
            position = (Integer)view.getTag();
            System.out.println(position);
            ShoppingCartBean shoppingCartBean = new ShoppingCartBean();
            shoppingCartBean.setShoppingName(titles[position]);
            shoppingCartBean.setPrice(Double.parseDouble(prices[position]));
            shoppingCartBean.setCount(1);
            shoppingCartBean.setImageUrl(icons[position]);
            shoppingCartBeanList.add(shoppingCartBean);
        }
    }

    class MyBaseAdapter extends BaseAdapter {
        //adapter先从getCount里确定数量，然后循环执行getView方法将条目一个一个绘制出来
        @Override
        public int getCount() {       //得到item的总数
            return titles.length;    //返回ListView Item条目代表的对象
        }

        @Override
        public Object getItem(int position) {
            return titles[position]; //返回item的数据对象
        }

        @Override
        public long getItemId(int position) {
            return position;         //返回item的id
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {//获取item中的View视图
            NotificationsFragment.ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(getActivity(), R.layout.activity_item, null);
                holder = new NotificationsFragment.ViewHolder();
                holder.title = convertView.findViewById(R.id.title);
                holder.price = convertView.findViewById(R.id.price);
                holder.iv = convertView.findViewById(R.id.iv);
                holder.shop_btn = convertView.findViewById(R.id.shop_btn);
                convertView.setTag(holder);
            } else {
                holder = (NotificationsFragment.ViewHolder) convertView.getTag();
            }
            holder.title.setText(titles[position]);
            holder.price.setText(prices[position]);
            holder.iv.setImageResource(icons[position]);
            holder.shop_btn.setTag(position);
            holder.shop_btn.setOnClickListener(NotificationsFragment.MyOnClickListener.getInstance());
            return convertView;
        }
    }
//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        binding = null;
//    }
}